import { defineStore } from 'pinia';
import { ElMessage } from 'element-plus';
// 修复：将命名导入改为默认导入
import request from '@/utils/request';
import type {
  Customer,
  CustomerQueryParams,
  CustomerCreateParams,
  CustomerUpdateParams,
  CustomerStatusParams
} from '@/types/base/BaseCustomer';

interface CustomerState {
  customers: Customer[];
  paginatedCustomers: Customer[];
  total: number;
  currentPage: number;
  pageSize: number;
  loading: boolean;
  queryParams: CustomerQueryParams;
}

export const useBaseCustomerStore = defineStore('BaseCustomer', {
  state: (): CustomerState => ({
    customers: [],
    paginatedCustomers: [],
    total: 0,
    currentPage: 1,
    pageSize: 10,
    loading: false,
    queryParams: {}
  }),

  actions: {
    setQueryParams(params: CustomerQueryParams) {
      this.queryParams = { ...params };
    },

    resetQueryParams() {
      this.queryParams = {};
      this.currentPage = 1;
      this.pageSize = 10;
    },

    setPagination(page: number, size: number) {
      this.currentPage = page;
      this.pageSize = size;
    },

    async getCustomers(params: CustomerQueryParams = {}) {
      this.loading = true;
      try {
        const response = await request<{
          list: Customer[];
          total: number;
        }>('/api/base/customers', {
          method: 'GET',
          params: { ...this.queryParams, ...params }
        });

        // 修复：通过 response.data 获取实际数据
        this.paginatedCustomers = response.data.list;
        this.total = response.data.total;
        return response.data.list;
      } catch (error) {
        ElMessage.error('获取客户列表失败');
        throw error;
      } finally {
        this.loading = false;
      }
    },

    async getCustomerDetail(id: number) {
      this.loading = true;
      try {
        const response = await request<Customer>(`/api/base/customers/${id}`, {
          method: 'GET'
        });
        return response;
      } catch (error) {
        ElMessage.error('获取客户详情失败');
        throw error;
      } finally {
        this.loading = false;
      }
    },

    async createCustomer(data: CustomerCreateParams) {
      this.loading = true;
      try {
        await request('/api/base/customers', {
          method: 'POST',
          data
        });
        ElMessage.success('客户创建成功');
      } catch (error) {
        ElMessage.error('客户创建失败');
        throw error;
      } finally {
        this.loading = false;
      }
    },

    async updateCustomer(data: CustomerUpdateParams) {
      this.loading = true;
      try {
        await request(`/api/base/customers/${data.customer_id}`, {
          method: 'PUT',
          data
        });
        ElMessage.success('客户更新成功');
      } catch (error) {
        ElMessage.error('客户更新失败');
        throw error;
      } finally {
        this.loading = false;
      }
    },

    async deleteCustomer(id: number) {
      this.loading = true;
      try {
        await request(`/api/base/customers/${id}`, {
          method: 'DELETE'
        });
        ElMessage.success('客户删除成功');
      } catch (error) {
        ElMessage.error('客户删除失败');
        throw error;
      } finally {
        this.loading = false;
      }
    },

    async toggleCustomerStatus(params: CustomerStatusParams) {
      this.loading = true;
      try {
        await request(`/api/base/customers/${params.customer_id}/status`, {
          method: 'PATCH',
          data: { is_active: params.is_active }
        });
        ElMessage.success(`${params.is_active ? '启用' : '禁用'}客户成功`);
      } catch (error) {
        ElMessage.error(`${params.is_active ? '启用' : '禁用'}客户失败`);
        throw error;
      } finally {
        this.loading = false;
      }
    }
  }
});
